<?php
/**
 * @author by bilee.
 * 时间: 16/6/17 上午6:08
 * 帮你跑腿APP管理后台
 */
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class AppmanagerController extends AdminbaseController
{
    function _initialize()
    {
        parent::_initialize();
    }
    //题库管理
    public function app_manager_list(){

        $app_manager_db = M("app_manager");
        $count = $app_manager_db->count();
        $page = $this->page($count, 20);

        $this->assign("datas",$app_manager_db->limit($page->firstRow . ',' . $page->listRows)->select());
        $this->assign("page", $page->show('Admin'));
        $this->display();
    }

//添加考试题库
    public function add_app_manager()
    {
        $app_manager_db = M("app_manager");
        if(IS_POST){
            $mobile = I('mobile');
            $user_name = I('user_name');
            $user_pass = md5('123456');
            if(!$mobile)
                $this->error("请输入手机号！", U("app_manager_list"));
            if(!$user_name)
                $user_name = $mobile;

            if(!preg_match("/^1[34578]{1}\d{9}$/",$mobile)){
                $this->error("请输入正确的手机号！");
            }
            $app_manager_obj = $app_manager_db->field('id')->where(array('mobile'=>$mobile))->find();

            if(!$app_manager_obj){
                $data = array( 'mobile'=>$mobile
                ,'user_name'=>$user_name
                ,'user_pass'=>$user_pass
                ,'create_time'=>date('Y-m-d H:i:s',time())
                );
                $add_ret = $app_manager_db->add($data);
                if ($add_ret) {
                    $this->success("新增成功！", U("app_manager_list"));
                } else {
                    $this->error("新增失败！", U("app_manager_list"));
                }
            }else{
                $this->error("手机号不能重复！");
            }

        }else{
            $this->display();
        }
    }
    //修改考试题库
    function edit_app_manager()
    {
        $app_manager_db = M("app_manager");
        if (IS_POST) {
            $mobile = I('mobile');
            $user_name = I('user_name');
            $old_user_pass = I('old_user_pass');
            $new_user_pass = I('new_user_pass');
            if(!$mobile)
                $this->error("请输入手机号！", U("app_manager_list"));
            if(!$user_name)
                $user_name = $mobile;

            if(!preg_match("/^1[34578]{1}\d{9}$/",$mobile)){
                $this->error("请输入正确的手机号！");
            }

            $app_manager_count = $app_manager_db->field('id')->where(array('mobile'=>$mobile))->count();

            if(intval($app_manager_count) > 1 ){
                $this->error("手机号不能重复，请重新输入！");
            }

            //初始化密码
            if(I('init_pass')==1){
                $data = array( 'mobile'=>$mobile
                ,'user_name'=>$user_name
                ,'user_pass'=>md5('123456')
                ,'last_login_time'=>date('Y-m-d H:i:s',time())
                );
            }elseif ($old_user_pass && $new_user_pass){//修改密码
                if($old_user_pass == $new_user_pass)
                    $this->error("新密码不能和原密码一样！");

                //检验输入旧密码是否与数据库中的一致
                $app_manager_obj = $app_manager_db->where(array('id'=>I('id'),'user_pass'=>sp_password($old_user_pass)))->find();
                if($app_manager_obj){
                    $data = array( 'mobile'=>$mobile
                    ,'user_name'=>$user_name
                    ,'user_pass'=>sp_password($new_user_pass)
                    ,'last_login_time'=>date('Y-m-d H:i:s',time())
                    );
                }else{
                    $this->error("原密码输入错误，请重新输入！");
                }
            }elseif(!$old_user_pass || !$new_user_pass){//修改用户姓名和手机号
                $app_manager_count = $app_manager_db->field('id')->where(array('mobile'=>$mobile,'user_name'=>$user_name))->count();
                if($app_manager_count == 0){
                    $data = array( 'mobile'=>$mobile
                    ,'user_name'=>$user_name
                    ,'last_login_time'=>date('Y-m-d H:i:s',time())
                    );
                }else{
                    $this->error("没有修改，请重新输入！");
                }
            }
            $up_ret = $app_manager_db->where(array('id'=>I('id')))->save($data);

            if ($up_ret) {
                $this->success("修改成功！", U("app_manager_list"));
            } else {
                $this->error("修改失败！", U("app_manager_list"));
            }
        } else {
            $ret = $app_manager_db->field('user_pass',true)->where(array('id' => I('id')))->find();
            $this->assign('datas', $ret);
            $this->display();
        }
    }
    //考试题库删除
    function deleteTest()
    {
        $ret = $this->test_model->delete(I('testdb_id'));
        if($ret){
            $this->success("删除成功！",U("test"));
        }else{
            $this->error("删除失败");
        }
    }

    /**
     * 禁用或启用
     */
    function app_manager_freezee(){
        $id = I('id');
        $free = I("freezee");

        if(isset($id) && $free){
            $app_users = M("app_manager");
            if($free==1){
                $rst =$app_users->where(array("id"=>$id))->save(array('user_status'=>0,'freeze_time'=>date('Y-m-d H:i:s',time()),'token'=>''));
            }elseif($free==2){
                $rst =$app_users->where(array("id"=>$id))->save(array('user_status'=>1,'freeze_time'=>date('Y-m-d H:i:s',time())));
            }

            if ($rst) {
                $this->assign("formget",I('formget'));
                if($free==1){
                    $this->success("用户禁用成功！", U('Appmanager/app_manager_list',array('formget'=>I('formget'))));
                }elseif ($free==2){
                    $this->success("用户启用成功！", U('Appmanager/app_manager_list',array('formget'=>I('formget'))));
                }
            } else {
                if($free==1)
                    $this->error("用户禁用成功！");
                elseif ($free==2)
                    $this->success("用户启用成功！");
            }
        } else {
            $this->error('数据传入失败！');
        }

    }

}